const bodyParser = require("body-parser");
const Mock = require("mockjs");
const data = Mock.mock({
  "address|5": [
    {
      address: "@county(true)",
      nickname: "@cname",
      tel: /1[3-9]\d{9}/,
      id: "@id",
    },
  ],
});

module.exports = (app) => {
  app.use(bodyParser.json());
  app.get("/api/address", (req, res) => {
    res.send({
      address: data.address[0],
      addressList: data.address,
    });
  });

  app.post("/api/address/add", (req, res) => {
    req.body.id = Mock.Random.id();
    data.address.unshift(req.body);
    res.send({
      code: 200,
      message: "添加成功",
    });
  });


	app.post("/api/address/edit", (req, res) => {
    const item = data.address.find(v => v.id === req.body.id);
		Object.assign(item, req.body)
    res.send({
      code: 200,
      message: "修改成功",
    });
  });
};
